-- Realistic Train v1.0.0
-- Author: stulleman

------------------------------------------------------------
----------------- Change Keybindings here ------------------
------------------------------------------------------------

local ENTER_EXIT_TRAIN_KEY = 71		-- Default: G (71) Don't use F because player will glitch
local THROTTLE_UP_KEY = 87			-- Default: W (87)
local THROTTLE_DOWN_KEY = 83		-- Default: S (83)
local BRAKE_TOGGLE_KEY = 32			-- Default: Space (32)
local CHANGE_DIRECTION = 17			-- Default: Control (17)

------------------------------------------------------------
------------------------------------------------------------
------------------------------------------------------------

local intrain = false
local train = 0

local enterDelay = 0
local brakeDelay = 0
local directionDelay = 0

local brake = false
local speed = 0
local throttle = 0
local direction = 1

local throttleSpeed = 0.5
local maxSpeed = 50
local acceleration = 0.02
local brakeForce = 0.03
local dragForce = 0.005

local realisticTrain = {}

function unload()

end

function init()

end

function tick()

	if(intrain) then
	
		drawGUI()
		controlTrain()
		
	end
	
	if(train ~= 0) then
		VEHICLE.SET_TRAIN_SPEED(train, direction * speed)
	end
	
	enterExitTrain()
	
end

function drawGUI()

	drawtext("Throttle: " .. string.format("%i", tostring(throttle)) .. " %", 0, 0.735, 255,255,255)
	drawtext("Speed: " .. string.format("%.1f", tostring(speed)) .. " mph", 0, 0.75,255,255,255)
	
	drawtext("Brake: " , 0, 0.765 ,255,255,255)
	
	if(brake) then
		drawtext("Armed", 0.03, 0.765,255,0,0)
	else
		drawtext("Released", 0.03, 0.765,0,255,0)
	end
	
	drawtext("Direction: " , 0, 0.78,255,255,255)
	
	if(direction > 0) then
		drawtext("Forward", 0.045, 0.78, 0,255,0)
	else
		drawtext("Backward", 0.0425, 0.78, 255,0,0)
	end
	
end

function controlTrain()
	
	-- if(intrain and CONTROLS.IS_CONTROL_PRESSED(2,)) then
		-- VEHICLE.SET_RENDER_TRAIN_AS_DERAILED(train, true) 
	-- end
	
	-- if(intrain and CONTROLS.IS_CONTROL_PRESSED(2,)) then
		-- VEHICLE.SET_RENDER_TRAIN_AS_DERAILED(train, false) 
	-- end
	
	-- Throttle up
	if(get_key_pressed(THROTTLE_UP_KEY) and throttle < 100) then	
		throttle =  throttle + throttleSpeed
	end
	
	-- Throttle down
	if(get_key_pressed(THROTTLE_DOWN_KEY) and throttle  > 0) then	
		throttle = throttle - throttleSpeed
	end
	
	-- Brake key
	if(get_key_pressed(BRAKE_TOGGLE_KEY) and (GAMEPLAY.GET_GAME_TIMER() - brakeDelay) > 500) then
		brakeDelay = GAMEPLAY.GET_GAME_TIMER()
		brake = not brake
	end
	
	-- Change direction key
	if(get_key_pressed(CHANGE_DIRECTION) and (GAMEPLAY.GET_GAME_TIMER() - directionDelay) > 500 and (speed <= 0.01 and speed >= -0.01)) then
		directionDelay = GAMEPLAY.GET_GAME_TIMER()
		direction = -direction
	end
	
	-- Forward
	-- if(direction > 0) then
	
		local speedLimit = (throttle / 100) * maxSpeed
		
		-- Speed
		if(speed < speedLimit) then
			speed = speed + acceleration
		end
		
		-- Brake
		if(brake and (speed - brakeForce) >= 0) then
			speed = speed - brakeForce
		end
		
		-- Drag
		if((speed - dragForce) >= 0) then
			speed = speed - dragForce
		end
		
	-- Backward
	-- else
	
		-- local speedLimit = (throttle / 100) * -maxSpeed
	
		-- -- Speed
		-- if(speed > speedLimit) then
			-- speed = speed - acceleration
		-- end
		
		-- -- Brake
		-- if(brake and (speed + brakeForce) <= 0) then
			-- speed = speed + brakeForce
		-- end
		
		-- -- Drag
		-- if((speed + dragForce) <= 0) then
			-- speed = speed + dragForce
		-- end
	
	-- end
	
	VEHICLE.SET_TRAIN_SPEED(train, direction * speed)
	
end

function enterExitTrain()

	local playerPed = PLAYER.PLAYER_PED_ID()
	
	-- Enter/Exit Train
	if(get_key_pressed(ENTER_EXIT_TRAIN_KEY) and (GAMEPLAY.GET_GAME_TIMER() - enterDelay) > 1000) then

		enterDelay = GAMEPLAY.GET_GAME_TIMER()
		
		-- Exit
		if(intrain) then	
		
			if (train ~= 0) then
			
				local playerOffset = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 2.3, -1.5, -1.0)
				ENTITY.SET_ENTITY_COORDS(playerPed, playerOffset.x, playerOffset.y, playerOffset.z, false, false, false, false)
				PED.SET_PED_INTO_VEHICLE(-1, train, -1)
				intrain = false
				
			end
		
		-- Enter
		else
			
			train = getCloseTrain(playerPed)
				
			if (train ~= 0) then
				
				local pedinTrain = VEHICLE.GET_PED_IN_VEHICLE_SEAT(train, -1)
				
				if(pedinTrain) then
					ENTITY.SET_ENTITY_COORDS(pedinTrain, 0, 0, 0, false,false,false,false)
				end
				
				PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
				intrain = true
			
			end
		end
	end
end

function drawtext(text, x, y, r, g, b)

	UI.SET_TEXT_FONT(0)
	UI.SET_TEXT_SCALE(0.3, 0.3)
	UI.SET_TEXT_COLOUR(r, g, b, 255)
	UI.SET_TEXT_WRAP(0, 1)
	UI.SET_TEXT_CENTRE(false)
	UI.SET_TEXT_DROPSHADOW(15, 15, 0, 0, 0)
	UI.SET_TEXT_EDGE(5, 0, 0, 0, 255)
	UI._SET_TEXT_ENTRY("STRING")
	UI._ADD_TEXT_COMPONENT_STRING(text)
	UI._DRAW_TEXT(x, y)
	
end

function getCloseTrain(playerPed)

	local Table,Count = PED.GET_PED_NEARBY_VEHICLES(playerPed, 1)
	
	for k,v in ipairs(Table) do		
		if( VEHICLE.IS_THIS_MODEL_A_TRAIN(ENTITY.GET_ENTITY_MODEL(v))) then	
			return v
		end
	end
	
	return 0
	
end

return realisticTrain